package com.example.hxpc.guidancemanagement.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.api.BaseApi;
import com.example.hxpc.guidancemanagement.model.JsonBase;
import com.example.hxpc.guidancemanagement.model.JsonFeedback;
import com.example.hxpc.guidancemanagement.tools.DialogUtil;
import com.example.hxpc.guidancemanagement.tools.MD5Util;
import com.example.hxpc.guidancemanagement.tools.SharePreferenceTools;
import com.example.hxpc.guidancemanagement.tools.ToastTools;
import com.example.hxpc.guidancemanagement.tools.dataTools.OkHttpTools;

/**
 * Created by tao on 2017/12/5.
 */

public class ChangePasswordActivity extends Activity implements View.OnClickListener,BaseApi{
    private ImageView img_add , img_back;
    private TextView te_title , te_sure ,te_sms;
    private EditText edt_phone , edt_old_password , edt_new_password;
    private OkHttpTools okHttpTools = new OkHttpTools();
//    private RelativeLayout rela_getCode;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_change_password);
        initView();
    }

    //初始化控件
    public void initView(){
        img_add = (ImageView) findViewById(R.id.add_mark);
        img_add.setVisibility(View.GONE);
        img_back = (ImageView) findViewById(R.id.back);
        img_back.setOnClickListener(this);
        te_title = (TextView) findViewById(R.id.te_title);
        te_title.setText("修改密码");
        te_sure = (TextView) findViewById(R.id.sure);
        te_sure.setVisibility(View.VISIBLE);
        te_sure.setOnClickListener(this);
        te_sms = (TextView) findViewById(R.id.te_sms);
        edt_phone = (EditText) findViewById(R.id.edt_phone);
        edt_old_password = (EditText) findViewById(R.id.edt_old_password);
        edt_new_password = (EditText) findViewById(R.id.edt_new_password);
//        edt_sms = (EditText) findViewById(R.id.edt_sms);
//        rela_getCode = (RelativeLayout) findViewById(R.id.rela_getCode);
//        rela_getCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sure:
                if (edt_phone.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"请输入手机号");
                }else if (edt_old_password.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"请输入新密码");
                }else if (edt_new_password.getText().toString().equals("")){
                    ToastTools.toastMsg(this,"确认密码不能空");
                }else if (!edt_phone.getText().toString().equals(SharePreferenceTools.getInstance(this).getString("phone"))){
                    ToastTools.toastMsg(this,"不是当前登录的账号");
                }else if (!edt_old_password.getText().toString().equals(edt_new_password.getText().toString())){
                    ToastTools.toastMsg(this,"两次密码不一致");
                }else{
                    okHttpTools.changePassword(this,edt_phone.getText().toString(), MD5Util.encrypt(edt_old_password.getText().toString()));
                }
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    public void backMsg(String success, final JsonBase jsonBase) {
        if (success.equals("true")){
            DialogUtil.show(this, R.string.logining);
            DialogUtil.dismiss2Msg("修改中。。。");
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(ChangePasswordActivity.this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                    ToastTools.toastMsg(ChangePasswordActivity.this,jsonBase.getMsg());
                }
            },2500);
        }else{
            ToastTools.toastMsg(this,jsonBase.getMsg());
        }
    }

    @Override
    public void backMsg(String success, JsonFeedback jsonFeedback) {

    }
}
